if orders and not orders.IsNewLimit then


function orders:IsNewLimit(transaction)
	if type(transaction) == "table" then
		return transaction.ACTION == "NEW_ORDER"
			and transaction.TYPE == "L"
			--and transaction.EXPIRY_DATE == "TODAY"
	end
end


function orders:IsKillOrder(transaction, key)
	if type(transaction) == "table" then
		return transaction.ACTION == "KILL_ORDER"
			and transaction.ORDER_KEY
			and (not key or transaction.ORDER_KEY == tostring(key))
	end
end


function orders:LimitTransaction(instrument)
	if not instrument then return end
	if type(instrument) == "string" then
		instrument = lib:GetInstrument(instrument)
	end
	--message("LimitTransaction " .. lib:toline(instrument))
	local transaction = self:NewTransaction(instrument, {
		ACTION = "NEW_ORDER",
		TYPE = "L",
		--OPERATION = instrument.side__ or instrument.OPERATION,
		--QUANTITY = tostring(instrument.qty__ or instrument.QUANTITY),
		--EXPIRY_DATE = "TODAY",
		--SECCODE = instrument.code or instrument.SECCODE or instrument.sec_code,
		--CLASSCODE = instrument.class_code or instrument.CLASSCODE,
		PRICE = instrument.PRICE,
		--CLIENT_CODE = instrument.CLIENT_CODE or instrument.client_code or lib:GetClientCode(),
		--ACCOUNT = instrument.ACCOUNT or instrument.account or lib:GetAccount(instrument.class_code or instrument.CLASSCODE),
		
		ex__ = {
			instrument = lib:GetInstrument(instrument),
			sl = instrument.sl__,
			lm = instrument.lm__,
			tp = instrument.tp__,
			qty = instrument.qty__,
			stop_qty = instrument.stop_qty__,
			--max_qty = instrument.max_qty__,
			--risk_limit = instrument.risk_limit__,
			credit = instrument.credit__,
			risk = instrument.risk__,
			side = instrument.side__,
			--max_lots = lib:GetMaxVolume(instrument, instrument.side__ == "B", instrument.lm__),
			--risk = lib:CalcRisk(instrument, instrument.lm__, instrument.sl__),
		}
	})		
		
	if not transaction then return end
	
	if instrument.lm__ then
		transaction.PRICE = lib:FormatPrice(instrument.lm__, transaction.ex__.instrument)
	end
	--if instrument.OPERATION then
		--transaction.ex__.instrument = lib:GetInstrument(instrument)
	--end
		
	return transaction
end


function orders:KillOrderTransaction(order)
	if not self:IsOrder(order) then return end
	
	local transaction = {
		ACTION="KILL_ORDER",
		CLASSCODE=order.class_code,
		SECCODE=order.sec_code,
		ORDER_KEY=tostring(order.order_num),
		ex__ = {
			instrument = lib:GetInstrument(order),
			key = tostring(order.order_num)
		}
	}
		
	self.log("KillOrderTransaction", transaction)

	return transaction
end


function orders:SendLimitTransaction(instrument)
	local transaction = self:LimitTransaction(instrument)
	--transaction.ACTION = "NEW_ORDER"
	--transaction.TYPE = "L"
	
	--[[
	local sp, bid, ask = lib:GetSpread(transaction.ex__.instrument)
	
	--lib:message(lib:tostring(sp))
	
	if sp then
		sp = tonumber(transaction.PRICE or 0)
		if transaction.OPERATION == "B" then
			if sp <= 0 or sp >= ask then
				transaction.PRICE = lib:FormatPrice(ask - transaction.ex__.instrument.min_price_step, transaction.ex__.instrument)
				--transaction.PRICE = lib:FormatPrice(ask + transaction.ex__.instrument.min_price_step, transaction.ex__.instrument)
				self.log("SendLimit shift buy price from " .. sp .. " to " .. transaction.PRICE)
			end
		else
			if sp <= bid then
				transaction.PRICE = lib:FormatPrice(bid + transaction.ex__.instrument.min_price_step, transaction.ex__.instrument)
				--transaction.PRICE = lib:FormatPrice(bid - transaction.ex__.instrument.min_price_step, transaction.ex__.instrument)
				self.log("SendLimit shift sell price from " .. sp .. " to " .. transaction.PRICE)
			end
		end
	--end
	--]]
	
	self:SetShiftPrice(transaction)
	
	if not transaction.PRICE then
		self.log(warning("SendLimitTransaction no PRICE - send market order"))
		transaction.TYPE = "M"
		transaction.PRICE = "0"
	end
	
	self.log("SendLimitTransaction", transaction)--, instrument, transaction)
	
	return transaction
end


function orders:SendLimit(instrument, next_transaction, next_transaction2)
	local transaction = self:SendLimitTransaction(instrument)
	
	return self:sendTransaction(transaction, next_transaction, next_transaction2)
end


function orders:ShiftLimit(order)
	local price = order.price
	
	if not price then
		self.log("ShiftLimit no price order", order)
	end
	
	order.price = nil		-- by spread or market
	--order.PRICE = nil		-- by spread or market
	
	if bit.test(order.flags, 2) then
		order.side__ = "S"
	else
		order.side__ = "B"
	end
	
	local transaction = self:SendLimitTransaction(order)
	
	if transaction.price
		and transaction.ex__
		and transaction.ex__.instrument
		and lib:NormalizePrice(transaction.price - price, transaction.ex__.instrument) ~= 0
	then
	--if transaction.price and transaction.price ~= price then
		self.log("ShiftLimit from " .. price .. " to " .. transaction.price, order)
		message("ShiftLimit order " .. order.order_num .. " from " .. price .. " to " .. transaction.price)
		
		--return orders:KillOrder(order, self:SendLimitTransaction(order))
		--return orders:SendLimit(order, orders:KillOrderTransaction(order))
		return orders:KillOrder(order, transaction)
	--else
		--message("ShiftLimit order " .. tostring(order.order_num) .. " equal price " .. tostring(price) .. " to " .. tostring(transaction.price))
	end
end


function orders:KillOrder(order, next_transaction)
	if not self:IsOrder(order) then return end
	
	self.log("KillOrder", order)
	
	for k, v in pairs(self.transactions) do
		if self:IsKillOrder(v, order.order_num) then
			self.log("KillOrder dublicate fail")
			return true
		end
	end
	
	local transaction = self:KillOrderTransaction(order)
	
	return self:sendTransaction(transaction, next_transaction)
end


end -- of file
